package view.mt4j;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.mt4j.MTApplication;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.sceneManagement.Iscene;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import command.Command;
import command.RemoteCommandRunner;

import controller.ApplicationController;

import view.interfaces.ApplicationView;
import view.interfaces.screen.View;
import view.interfaces.screen.menu.AppHomeView;
import view.interfaces.screen.menu.JoinProjectMenuView;
import view.interfaces.screen.menu.ManageProjectsMenuView;
import view.interfaces.screen.menu.NewProjectMenuView;
import view.interfaces.screen.project.ProjectView;
import view.interfaces.workspace.WorkspaceView;
import view.mt4j.scene.menu.AppHomeScene;
import view.mt4j.scene.menu.JoinProjectMenuScene;
import view.mt4j.scene.menu.ManageProjectsMenuScene;
import view.mt4j.scene.menu.NewProjectMenuScene;
import view.mt4j.scene.project.ProjectScene;
import view.mt4j.workspace.WorkspaceViewImpl;

import model.ApplicationModel;
import model.Workspace;

public class MT4JApplicationView extends JFrame implements ApplicationView {

	private static final long serialVersionUID = 6471122087311630843L;

	private static final Logger log = LoggerFactory
			.getLogger(MT4JApplicationView.class);

	private MTApplication mtApplication;
	private ApplicationModel model;
	private ApplicationController applicationController;

	private AppHomeScene appHomeScene;
	private NewProjectMenuScene newProjectMenuScene;
	private ManageProjectsMenuScene manageProjectsMenuScene;
	private JoinProjectMenuScene joinProjectMenuScene;
	private ProjectScene projectScene;
	//private JFrame jFrame;

	public MT4JApplicationView(ApplicationModel model) {
		this.model = model;
		setLayout(new BorderLayout());
		setUndecorated(true);
		//jFrame = initFrame();
		mtApplication = new MTApplication() {

			private static final long serialVersionUID = 4361925412828703219L;

			@Override
			public void startUp() {
				mtApplication.invokeLater(new Runnable() {
					
					@Override
					public void run() {
						log.debug("MT4JApplicationView.startUp()");
						initScenes();
					}
				});
				
			}

			private void initScenes() {
				log.debug("initScenes ...");
				// first view will be opened when application starts
				mtApplication.addScene(new AbstractScene(mtApplication,
						"splash") {
				});

				appHomeScene = new AppHomeScene(mtApplication,
						MT4JApplicationView.this.model);
				mtApplication.addScene(appHomeScene);
				newProjectMenuScene = new NewProjectMenuScene(mtApplication,
						MT4JApplicationView.this.model);
				mtApplication.addScene(newProjectMenuScene);
				joinProjectMenuScene = new JoinProjectMenuScene(mtApplication,
						MT4JApplicationView.this.model);
				mtApplication.addScene(joinProjectMenuScene);
				manageProjectsMenuScene = new ManageProjectsMenuScene(
						mtApplication, MT4JApplicationView.this.model);
				mtApplication.addScene(manageProjectsMenuScene);
				projectScene = new ProjectScene(mtApplication,
						MT4JApplicationView.this.model);
				mtApplication.addScene(projectScene);
				log.debug("initScenes DONE");
			}

		};
		displayMtApplication();
	}

	private void displayMtApplication() {
		log.debug("displayMtApplication ...");
		// Important for registering the Windows 7 Touch
		mtApplication.frame = this;
		mtApplication.init();
		JPanel pane = new JPanel(new GridLayout(0, 1));
		pane.add(mtApplication);
		final Container content = this.getContentPane();
		content.add(pane, BorderLayout.CENTER);
		this.setSize(this.getToolkit().getScreenSize());
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		log.debug("displayMtApplication DONE");
	}

	private JFrame initFrame() {
		JFrame frame = new JFrame();
		frame.setLayout(new BorderLayout());
		frame.setUndecorated(true);
		return frame;
	}

	public void setApplicationController(
			final ApplicationController applicationController) {
		mtApplication.invokeLater(new Runnable() {

			@Override
			public void run() {
				log.debug("setApplicationController ...");
				MT4JApplicationView.this.applicationController = applicationController;
				appHomeScene.setController(applicationController);
				newProjectMenuScene.setController(applicationController);
				joinProjectMenuScene.setController(applicationController);
				manageProjectsMenuScene.setController(applicationController);
				projectScene.setController(applicationController);
				log.debug("setApplicationController DONE");
				showView(appHomeScene);
			}
		});

	}

	@Override
	public NewProjectMenuView getNewProjectMenuView() {
		return newProjectMenuScene;
	}

	@Override
	public ManageProjectsMenuView getManageProjectsMenuView() {
		return manageProjectsMenuScene;
	}

	@Override
	public JoinProjectMenuView getJoinProjectMenuView() {
		return joinProjectMenuScene;
	}

	@Override
	public ProjectView getProjectView() {
		return projectScene;
	}

	@Override
	public AppHomeView getAppHomeView() {
		return appHomeScene;
	}

	@Override
	public void showView(final View view) {
		mtApplication.invokeLater(new Runnable() {

			@Override
			public void run() {
				log.debug("showView {}", view.getClass().getSimpleName());
				mtApplication.pushScene();
				mtApplication.changeScene((Iscene) view);
			}
		});

	}

	@Override
	public void runRemoteCommand(Command command) {
		mtApplication.invokeLater(new RemoteCommandRunner(applicationController,
				command));
	}

}
